//
//  mm_subViewForPicRecode.m
//  HondaPro_2
//
//  Created by Eric on 13-7-2.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_subViewForPicRecode.h"
#import "mm_selfDefinedTableViewCellOfSubViewForPicRecode.h"
#import "MainMenuDataModel.h"

@interface mm_subViewForPicRecode ()

@property(nonatomic,strong)NSArray *datas;

@end

@implementation mm_subViewForPicRecode

@synthesize tableViewDis;
@synthesize nRowSel = _nRowSel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticLbl];
        [self createTableViewDis];
    }
    return self;
}


-(NSArray *)datas{
    if (_datas == nil) {
        MainMenuDataModel *mainModel = [MainMenuDataModel sharedMainMenuDataModel];
        _datas = mainModel.arrOutCheck;
    }
    return _datas;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticLbl
{
    NSArray *arrForStaticLblsKeys = [NSArray arrayWithObjects:
                                     @"bottom",
                                     @"idIndex",
                                     @"type",
                                     @"number",
                                     @"remarks", nil];
    
    NSDictionary *dicForStaticLblsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSValue valueWithCGRect:CGRectMake(0, 0, self.frame.size.width, 50)],@"bottom",
                                           [NSValue valueWithCGRect:CGRectMake(40, 0, 100, 50)],@"idIndex",
                                           [NSValue valueWithCGRect:CGRectMake(141, 0, 100, 50)],@"type",
                                           [NSValue valueWithCGRect:CGRectMake(242, 0, 100-30, 50)],@"number",
                                           [NSValue valueWithCGRect:CGRectMake(343+40, 0, self.frame.size.width-343-60-40, 50)],@"remarks", nil];
    
    NSDictionary *dicForStaticLblsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                          @"",@"bottom",
                                          @"编号",@"idIndex",
                                          @"类型",@"type",
                                          @"数量",@"number",
                                          @"备注",@"remarks", nil];
    
    for (int i = 0; i<[arrForStaticLblsKeys count]; i++)
    {
        NSString *strKey = [arrForStaticLblsKeys objectAtIndex:i];
        NSString *txt = [dicForStaticLblsText objectForKey:strKey];
        CGRect rect = (CGRect)[[dicForStaticLblsRect objectForKey:strKey] CGRectValue];
        
        UILabel *lbl = [[UILabel alloc] initWithFrame:rect];
        lbl.text = txt;
        
        lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:0.96];
        lbl.textColor = [UIColor grayColor];
        lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
        lbl.numberOfLines = 2;
        
        /*just for test*/
        if ([strKey isEqualToString:@"bottom"])
        {
            
        }
        else
        {
            lbl.backgroundColor = [UIColor clearColor];
        }
        /*end of just for test*/
        
        [self addSubview:lbl];
    }
}

-(void)createTableViewDis
{
    self.tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50) style:UITableViewStylePlain];
    
    self.tableViewDis.backgroundColor = [UIColor clearColor];
    
    self.tableViewDis.dataSource = self;
    self.tableViewDis.delegate = self;
    
    [self addSubview:self.tableViewDis];
}

#pragma mark - 刷新控件内容

-(void)freshenViewDis
{
    [tableViewDis reloadData];
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 3+self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    mm_selfDefinedTableViewCellOfSubViewForPicRecode *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[mm_selfDefinedTableViewCellOfSubViewForPicRecode alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
            
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    if (row<self.datas.count)
    {
        NSDictionary *dataDic = self.datas[row];
        cell.dataDic = dataDic;
        
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        cell.dataDic = nil;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([indexPath row] < self.datas.count)
    {
        self.nRowSel = indexPath.row;
    }
    else
    {
        self.nRowSel = -1;
    }
}

//哪几行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.canEdit) {
        if (indexPath.row<self.datas.count)
        {
            return YES;
        }
    }    
    return NO;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//当 tableview 为 editing 时,左侧按钮的 style
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{}

#pragma mark - 出现删除按钮时，点击按钮时的操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainMenuDataModel *mainModel = [MainMenuDataModel sharedMainMenuDataModel];
    NSMutableDictionary *deleteData = mainModel.arrOutCheck[indexPath.row];
    if (![deleteData[@"STATUS"] isEqualToString:@"A"]) {//如果是新增的
        [deleteData setObject:@"D" forKey:@"STATUS"];
        [mainModel.arrOutCheckDeleted addObject:deleteData];
    }
    [mainModel.arrOutCheck removeObject:deleteData];
    self.datas = nil;
    [self freshenViewDis];
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(deleteData:atIndexPath:)]) {
        [self.delegate deleteData:deleteData atIndexPath:indexPath];
    }
}


@end
